package com.mcxx.modules.miniSalvation.order.service.read;

import com.mcxx.common.exception.BizException;
import com.mcxx.constant.DictConstant;
import com.mcxx.formulcount.param.FormulCountParam;
import com.mcxx.modules.base.service.read.DictionaryReadService;
import com.mcxx.modules.miniSalvation.order.dao.read.StandardReadDao;
import com.mcxx.modules.miniSalvation.order.dto.SalvationStandardDTO;
import com.mcxx.modules.miniSalvation.order.entity.StandardEntity;
import com.mcxx.modules.system.dao.read.SalvationRuleReadDao;
import com.mcxx.modules.system.param.StandardParam;
import com.mcxx.util.Constant;
import com.yinhai.ta404.core.restservice.requestbean.PageParam;
import com.yinhai.ta404.core.restservice.resultbean.Page;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.service.BaseService;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import com.yinhai.ta404.module.dict.entity.TaDictPo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;


@Service("salvationStandardReadService")
@TaTransactional(readOnly = true)
public class StandardReadService extends BaseService {

    @Autowired
    private StandardReadDao standardReadDao;
    @Autowired
    private SalvationRuleReadDao salvationRuleReadDao;
    @Autowired
    private DictionaryReadService dictionaryReadService;

    public StandardEntity querySalvationStandard(StandardEntity standardEntiy) {
        return standardReadDao.querySalvationStandard(standardEntiy);
    }


    public Page getSalvationStandardList(StandardParam param, PageParam pageParam) {
        standardReadDao.beginPager(pageParam);
        List<StandardEntity> list = standardReadDao.getSalvationStandardList(param);
        return standardReadDao.endPager(list);
    }

    public List<StandardEntity> getSalvationStandards(StandardParam param) {
        List<StandardEntity> list = standardReadDao.getSalvationStandardList(param);
        return list;
    }


    public List<StandardEntity> getByParam(StandardParam standardParam) {
        return standardReadDao.getByParam(standardParam);
    }


    public SalvationStandardDTO getStandardGear(String standType, UserAccountVo userAccountVo, Double familyRevenue, String businessType){


//        String areaCode = Constant.UserLevel.COUNTY.equals(userAccountVo.getAreaLevel()) ? userAccountVo.getAreaCode() : userAccountVo.getParentId();
        StandardParam standardParam = new StandardParam();
        standardParam.setStandType(standType);

        standardParam.setAreaCode(userAccountVo.getAreaCode().substring(0, 6)+ "000000");
        standardParam.setBusinessType(businessType);
        standardParam.setStatus(Constant.StandardStatus.ENABLE);

        standardParam.setGearCondition(familyRevenue < 0 ? 0 : familyRevenue);

        List<StandardEntity> standardList = this.getByParam(standardParam);
        if(CollectionUtils.isEmpty(standardList)){
            throw new BizException("-1","没有找到符合救助该人员的政策！请您检查救助政策配置是否正确！");
        }

        StandardEntity standardEntity = standardList.get(0);

        SalvationStandardDTO curSalvationDTO = new SalvationStandardDTO();
        BeanUtils.copyProperties(standardEntity, curSalvationDTO);
        curSalvationDTO.setType(Constant.AssistantStandardType.gearStandardMap.get(curSalvationDTO.getType()));
        TaDictPo dictPo = dictionaryReadService.getCodeCache(DictConstant.GuaranteeType, curSalvationDTO.getType());
        curSalvationDTO.setTypeName(dictPo.getLabel());
        return curSalvationDTO;
    }


    public List<StandardEntity> querySalvationByPid(String pId){return standardReadDao.querySalvationByPid(pId);}


    public int getOrderByGear(String type, String gearCode, String areaCode){

        String countType = gearCode;
        if("1".equals(type)){

            countType = Constant.AssistantStandardType.gearStandardMap.get(gearCode);
        }
        areaCode = areaCode.substring(0,6);

        return standardReadDao.getOrderByGear(type,countType,areaCode);
    }




    public String getStandardMoney(String areaCode, String businessType, String type){
        return standardReadDao.getStandardMoney(areaCode, businessType, type);
    }


    public Map<String, BigDecimal> getMaxMinStandardMoney(String areaCode, String businessType){
        return standardReadDao.getMaxMinStandardMoney(areaCode, businessType);
    }



    public List<StandardEntity> getStandardForGroup(String standardId){
        return standardReadDao.getStandardForGroup(standardId);
    }



    public int countSalvaLSType(String areaCode, String id){
        return standardReadDao.countSalvaLSType(areaCode,id);
    }


    public StandardEntity selectStandardById(String id) {
        return standardReadDao.selectStandardById(id);
    }
}
